#!/bin/bash

#usage bash bras_control.sh start

if [ -z "$(id | grep "uid=0")" ];then
	echo "You're not the root!"
	exit 1
fi

RESULT_FILE="../log/connect_result"
LOG_FILE="/var/log/bras_log"
BRAS_START_PID_FILE="/tmp/bras_start_pid"
INTERVAL=2.5
TIMES_LEFT=5

UNIT="$(sed -n "/unit/"p ../config/extra_options | grep -o [0-9])"

check_xl2tpd()
{
	XL2TPD_PATH=$(which xl2tpd)


	if [ -x $XL2TPD_PATH ];then
		return 0
	else
		return 1
	fi

}

generate_xl2tpd_config_file()
{
	cat >/etc/xl2tpd/njubras.conf <<XL2TPD_CONFIGURE_FILE
[global]
access control = no
port = 1701
[lac njubras]
pppoptfile = /etc/ppp/peers/njubras
ppp debug = yes
XL2TPD_CONFIGURE_FILE

	if [ $# -eq 1 ];then
		echo "lns = $(sed -n '1p' ../config/bras_ip)" >>/etc/xl2tpd/njubras.conf
	elif [ $# -eq 2 ];then
		echo "lns = $(sed -n '2p' ../config/bras_ip)" >>/etc/xl2tpd/njubras.conf
	fi

}

generate_pppd_config_file()
{
	cat >/etc/ppp/peers/njubras <<PPPD_CONFIGURE_FILE
remotename njubras
user $1
password $2
logfile $LOG_FILE
PPPD_CONFIGURE_FILE

	cat ../config/extra_options >>/etc/ppp/peers/njubras
}

log_analyze()
{

	if [ -n "$(cat $LOG_FILE | grep -i success)" ];then
		echo "Connect successfully." >"$RESULT_FILE"
		return 0
	fi

	if [ -n "$(cat $LOG_FILE | grep -i unreachable)" ];then
		if [ "$TIMES_LEFT" -eq 0 ];then
			echo "Network unreachable." >"$RESULT_FILE"
		fi

		return 1
	fi

	if [ -z "$(cat $LOG_FILE | grep -i pppd)" ];then
		if [ "$TIMES_LEFT" -eq 0 ];then
			echo "Can't connect to server." >"$RESULT_FILE"
		fi

		return 2
	fi

	if [ -n "$(cat $LOG_FILE | grep -i pppd)" ];then
		if [ "$TIMES_LEFT" -eq 0 ];then
			echo "Authentication failed." >"$RESULT_FILE"
		fi

		return 3
	fi
	
	if [ "$TIMES_LEFT" -eq 0];then
		echo "Unknown error." >"$RESULT_FILE"
	fi

	return 4

}



if [ "$1" = "start" ] || [ "$1" = "stop" ];then
	
	check_xl2tpd
	
	if [ $? -ne 0 ];then
		echo "xl2tpd needed, but not found." >"$RESULT_FILE"
		exit 1
	fi

fi

if [ ! -d ../log ];then
	mkdir ../log
fi

case "$1" in
	start	)
		echo "$$">$BRAS_START_PID_FILE
		
		pkill -KILL xl2tpd

		if [ ! -d /var/run/xl2tpd ];then
			mkdir /var/run/xl2tpd
		fi
	
		echo "?" >"$RESULT_FILE"
		chmod 644 "$RESULT_FILE"

		generate_xl2tpd_config_file $2 $4
		generate_pppd_config_file $2 $3 $4

		#syslog_handle "backup"
		xl2tpd -c /etc/xl2tpd/njubras.conf -D 2>$LOG_FILE &
		sleep 0.1
		
		if [ -n "$(pgrep xl2tpd)" ];then
			echo 'c njubras' >>/var/run/xl2tpd/l2tp-control
		else
			echo "Can't start xl2tpd" >"$RESULT_FILE"
		fi

		while [ "$TIMES_LEFT" -ge 0 ];do
			sleep $INTERVAL
			log_analyze
			
			CONNECT_RESULT=$?

			if [ "$CONNECT_RESULT" -eq 0 ];then
				break
			fi
			
			if [ "$TIMES_LEFT" -eq 0 ] && [ "$CONNECT_RESULT" -ne 0 ];then
				rm -f $BRAS_START_PID_FILE 2>/dev/null
				exit 1
			fi

			TIMES_LEFT=$(($TIMES_LEFT-1))
		done

		if [ -n "$(grep yes ../config/add_routes.conf)" ];then
			bash routes_change.sh add $4
		fi

		rm -f  $BRAS_START_PID_FILE 2>/dev/null
		;;
	stop	)
		#rsyslog_config "default_config"
		
		if [ -n "$(ps -e | grep xl2tpd)" ];then
			echo "d njubras" >>/var/run/xl2tpd/l2tp-control
		fi
	
		pkill -KILL xl2tpd

		if [ -n "$(grep yes ../config/add_routes.conf)" ];then
			bash routes_change.sh delete $2
		fi

		chmod 644 "$RESULT_FILE"
		echo "?" >"$RESULT_FILE"

		;;
	result	)
		cat "$RESULT_FILE"
		;;
	kill	)

		if [ -e "$BRAS_START_PID_FILE" ];then
			OLD_BRAS_START_PID=$(cat "$BRAS_START_PID_FILE")

			kill -KILL "$OLD_BRAS_START_PID" 2>/dev/null
			pkill -KILL xl2tpd
			rm -f "$BRAS_START_PID_FILE" 2>/dev/null
		fi
		;;
	*	)
		echo "Unkown option"
		;;

esac

exit 0


